package com.xuhao.examai.question.interfaces.controllers;

import com.xuhao.examai.question.domain.models.Question;
import com.xuhao.examai.common.ResultVO;
import com.xuhao.examai.common.PageResult;
import com.xuhao.examai.question.application.services.QuestionService;
import com.xuhao.examai.question.application.dto.QuestionDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "题目管理", description = "题目相关API")
@RestController
@RequestMapping("/api/questions")
@RequiredArgsConstructor
public class QuestionController {

	private final QuestionService questionService;

	@Operation(summary = "创建题目")
	@PostMapping
	public ResultVO<Question> create(@Valid @RequestBody QuestionDTO questionDTO) {
		Question question = convertToEntity(questionDTO);
		return ResultVO.success(questionService.create(question));
	}

	@Operation(summary = "更新题目")
	@PutMapping("/{id}")
	public ResultVO<Question> update(@PathVariable Long id, @Valid @RequestBody QuestionDTO questionDTO) {
		Question question = convertToEntity(questionDTO);
		question.setId(id);
		return ResultVO.success(questionService.update(question));
	}

	@Operation(summary = "删除题目")
	@DeleteMapping("/{id}")
	public ResultVO<Void> delete(@PathVariable Long id) {
		questionService.delete(id);
		return ResultVO.success();
	}

	@Operation(summary = "获取题目详情")
	@GetMapping("/{id}")
	public ResultVO<Question> get(@PathVariable Long id) {
		return ResultVO.success(questionService.get(id));
	}

	@Operation(summary = "根据考试ID获取题目列表")
	@GetMapping("/exam/{examId}")
	public ResultVO<List<Question>> listByExamId(@PathVariable Long examId) {
		return ResultVO.success(questionService.listByExamId(examId));
	}

	@Operation(summary = "分页查询题目")
	@GetMapping
	public ResultVO<PageResult<Question>> listByPage(
			@RequestParam(defaultValue = "1") Integer current,
			@RequestParam(defaultValue = "10") Integer size,
			@RequestParam(required = false) String keyword,
			@RequestParam(required = false) String questionType,
			@RequestParam(required = false) String difficulty) {
		return ResultVO.success(questionService.listByPage(current, size, keyword, questionType, difficulty));
	}

	@Operation(summary = "根据知识点查询题目")
	@GetMapping("/knowledge-point/{knowledgePoint}")
	public ResultVO<List<Question>> listByKnowledgePoint(@PathVariable String knowledgePoint) {
		return ResultVO.success(questionService.listByKnowledgePoint(knowledgePoint));
	}

	@Operation(summary = "根据难度查询题目")
	@GetMapping("/difficulty/{difficulty}")
	public ResultVO<List<Question>> listByDifficulty(@PathVariable String difficulty) {
		return ResultVO.success(questionService.listByDifficulty(difficulty));
	}

	@Operation(summary = "根据类型查询题目")
	@GetMapping("/type/{questionType}")
	public ResultVO<List<Question>> listByQuestionType(@PathVariable String questionType) {
		return ResultVO.success(questionService.listByQuestionType(questionType));
	}

	@Operation(summary = "批量创建题目")
	@PostMapping("/batch")
	public ResultVO<List<Question>> batchCreate(@Valid @RequestBody List<QuestionDTO> questionDTOs) {
		List<Question> questions = questionDTOs.stream()
				.map(this::convertToEntity)
				.toList();
		return ResultVO.success(questionService.batchCreate(questions));
	}

	@Operation(summary = "验证题目答案")
	@PostMapping("/{id}/validate")
	public ResultVO<Boolean> validateAnswer(@PathVariable Long id, @RequestParam String answer) {
		return ResultVO.success(questionService.validateAnswer(id, answer));
	}

	/**
	 * 将DTO转换为实体
	 */
	private Question convertToEntity(QuestionDTO dto) {
		Question question = new Question();
		question.setId(dto.getId());
		question.setExamId(dto.getExamId());
		question.setQuestionType(dto.getQuestionType());
		question.setContent(dto.getContent());
		question.setOptions(dto.getOptions());
		question.setCorrectAnswer(dto.getCorrectAnswer());
		question.setScore(dto.getScore());
		question.setKnowledgePoint(dto.getKnowledgePoint());
		question.setDifficulty(dto.getDifficulty());
		return question;
	}
}
